package com.example.imchat.model.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.imchat.model.bean.GroupInfo;
import com.example.imchat.model.bean.InvivationInfo;
import com.example.imchat.model.bean.UserInfo;
import com.example.imchat.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

/*
* 邀请信息操作类
* */
public class InviteTableDao {
    private DBHelper mHelper;

    public InviteTableDao(DBHelper helper) {
        mHelper = helper;
    }

    //添加邀请
    public void addInvitation(InvivationInfo invationInfo){
        //建立连接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //执行语句
        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_REASON,invationInfo.getReason()); //原因
        values.put(InviteTable.COL_STATUS, invationInfo.getStatus().ordinal()); //枚举的序号 从0开始
        UserInfo user  = invationInfo.getUser();
        if (user !=null){
            //联系人
            values.put(InviteTable.COL_USER_HXID, invationInfo.getUser().getHxid());
            values.put(InviteTable.COL_USER_NAME, invationInfo.getUser().getName());
        }else {
            //群组
            values.put(InviteTable.COL_GROUP_NAME,invationInfo.getGroup().getGroupName());
            values.put(InviteTable.COL_GROUP_HXID,invationInfo.getGroup().getGroupId());
            values.put(InviteTable.COL_USER_HXID,invationInfo.getGroup().getInvatePerson());
        }
        db.replace(InviteTable.TAB_NAME,null,values);
    }

    //获取所有邀请信息
    public List<InvivationInfo> getInvitations(){
        //获取链接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //执行语句
        String sql = " select * from "+ InviteTable.TAB_NAME;
        Cursor cursor = db.rawQuery(sql, null);

        List<InvivationInfo> invationInfos = new ArrayList<>();
        while (cursor.moveToNext()){
            InvivationInfo invationInfo = new InvivationInfo();
            invationInfo.setReason((cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON))));
            invationInfo.setStatus(int2InviteStatus(cursor.getInt(cursor.getColumnIndex(InviteTable.COL_STATUS))));

            String groupId = cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID));
            if (groupId == null){
                //联系人邀请信息
                UserInfo userInfo = new UserInfo();

                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));
                userInfo.setName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                userInfo.setNick(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                invationInfo.setUser(userInfo);
            }else {
                //群组邀请信息
                GroupInfo groupInfo  = new GroupInfo();

                groupInfo.setGroupId(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID)));
                groupInfo.setGroupName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_NAME)));
                groupInfo.setInvatePerson(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));
                invationInfo.setGroup(groupInfo);
            }
            invationInfos.add(invationInfo);
        }
        //关闭资源
            cursor.close();
        //返回信息
        return invationInfos;
    }

        //将int类型状态转换为邀请的状态
    private InvivationInfo.InvitationStatus int2InviteStatus(int intStatus) {
        if (intStatus == InvivationInfo.InvitationStatus.INVITE_DECLINED.ordinal()) {
            return InvivationInfo.InvitationStatus.INVITE_DECLINED;
        }
        if (intStatus == InvivationInfo.InvitationStatus.AUTO_NEW_GROUP_INVITE.ordinal()) {
            return InvivationInfo.InvitationStatus.AUTO_NEW_GROUP_INVITE;
        }

        if (intStatus == InvivationInfo.InvitationStatus.NEW_INVITE.ordinal()) {
            return InvivationInfo.InvitationStatus.NEW_INVITE;
        }
        if (intStatus == InvivationInfo.InvitationStatus.INVITE_ACCEPT.ordinal()) {
            return InvivationInfo.InvitationStatus.INVITE_ACCEPT;
        }
        if (intStatus == InvivationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER.ordinal()) {
            return InvivationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER;
        }

        if (intStatus == InvivationInfo.InvitationStatus.NEW_GROUP_INVITE.ordinal()) {
            return InvivationInfo.InvitationStatus.NEW_GROUP_INVITE;
        }
        if (intStatus == InvivationInfo.InvitationStatus.NEW_GROUP_APPLICATION.ordinal()) {
            return InvivationInfo.InvitationStatus.NEW_GROUP_APPLICATION;
        }

        if (intStatus == InvivationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED.ordinal()) {
            return InvivationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED;
        }
        if (intStatus == InvivationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED.ordinal()) {
            return InvivationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED;
        }
        if (intStatus == InvivationInfo.InvitationStatus.GROUP_INVITE_DECLINED.ordinal()) {
            return InvivationInfo.InvitationStatus.GROUP_INVITE_DECLINED;
        }
        if (intStatus == InvivationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED.ordinal()) {
            return InvivationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED;
        }
        if (intStatus == InvivationInfo.InvitationStatus.GROUP_ACCEPT_INVITE.ordinal()) {
            return InvivationInfo.InvitationStatus.GROUP_ACCEPT_INVITE;
        }
        if (intStatus == InvivationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION.ordinal()) {
            return InvivationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION;
        }
        if (intStatus == InvivationInfo.InvitationStatus.GROUP_REJECT_APPLICATION.ordinal()) {
            return InvivationInfo.InvitationStatus.GROUP_REJECT_APPLICATION;
        }
        if (intStatus == InvivationInfo.InvitationStatus.GROUP_REJECT_INVITE.ordinal()) {
            return InvivationInfo.InvitationStatus.GROUP_REJECT_INVITE;
        }
        return null;
    }


    //删除邀请
    public void removeInvitation(String hxId){
        if (hxId == null){
            return;
        }
        //获取链接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //执行语句
        db.delete(InviteTable.TAB_NAME,InviteTable.COL_USER_HXID + "=?",new String[]{hxId});

    }

    //更新邀请状态
    public  void updateInvitationStatus(InvivationInfo.InvitationStatus invitationStatus , String hxId){
        if (hxId == null){
            return;
        }
        //获取链接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //执行语句
        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_STATUS, invitationStatus.ordinal());

        db.update(InviteTable.TAB_NAME,values,InviteTable.COL_USER_HXID + "=?",new String[]{hxId});

    }
}
